home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / indent.el < prev    next >
Encoding:
Text File  |  1995-05-12  |  9.2 KB  |  281 lines

  1. ;;; indent.el --- indentation commands for XEmacs
  2. ;; Keywords: lisp languages tools
  3.  
  4. ;; Copyright (C) 1985, 1992, 1993 Free Software Foundation, Inc.
  5.  
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Synched up with: FSF 19.28.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; Commands for making and changing indentation in text.  These are
  29. ;; described in the XEmacs Reference Manual.
  30.  
  31. ;;; Code:
  32.  
  33. ;jwz: this is preloaded so don't ;;;###autoload
  34. (defvar indent-line-function 'indent-to-left-margin
  35.   "Function to indent current line.")
  36.  
  37. (defun indent-according-to-mode ()
  38.   "Indent line in proper way for current major mode."
  39.   (interactive)
  40.   (funcall indent-line-function))
  41.  
  42. (defun indent-for-tab-command ()
  43.   "Indent line in proper way for current major mode."
  44.   (interactive)
  45.   (if (eq indent-line-function 'indent-to-left-margin)
  46.       (insert-tab)
  47.     (funcall indent-line-function)))
  48.  
  49. (defun insert-tab ()
  50.   (if abbrev-mode
  51.       (expand-abbrev))
  52.   (if indent-tabs-mode
  53.       (insert ?\t)
  54.     (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
  55.  
  56. (defun indent-rigidly (start end arg)
  57.   "Indent all lines starting in the region sideways by ARG columns.
  58. Called from a program, takes three arguments, START, END and ARG."
  59.   (interactive "r\np")
  60.   (save-excursion
  61.     (goto-char end)
  62.     (setq end (point-marker))
  63.     (goto-char start)
  64.     (or (bolp) (forward-line 1))
  65.     (while (< (point) end)
  66.       (let ((indent (current-indentation))
  67.         eol-flag)
  68.     (save-excursion
  69.       (skip-chars-forward " \t")
  70.       (setq eol-flag (eolp)))
  71.     (or eol-flag
  72.         (indent-to (max 0 (+ indent arg)) 0))
  73.     (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  74.       (forward-line 1))
  75.     (move-marker end nil)
  76.     (setq zmacs-region-stays nil)))
  77.  
  78. ;; This is the default indent-line-function,
  79. ;; used in Fundamental Mode, Text Mode, etc.
  80. (defun indent-to-left-margin ()
  81.   (or (= (current-indentation) left-margin)
  82.       (let (epos)
  83.     (save-excursion
  84.      (beginning-of-line)
  85.      (delete-region (point)
  86.             (progn (skip-chars-forward " \t")
  87.                    (point)))
  88.      (indent-to left-margin)
  89.      (setq epos (point)))
  90.     (if (< (point) epos)
  91.         (goto-char epos)))))
  92.  
  93. (defvar indent-region-function nil
  94.   "Function which is short cut to indent region using indent-according-to-mode.
  95. A value of nil means really run indent-according-to-mode on each line.")
  96.  
  97. (defun indent-region (start end column)
  98.   "Indent each nonblank line in the region.
  99. With no argument, indent each line using `indent-according-to-mode',
  100. or use `indent-region-function' to do the whole region if that's non-nil.
  101. If there is a fill prefix, make each line start with the fill prefix.
  102. With argument COLUMN, indent each line to that column.
  103. Called from a program, takes three args: START, END and COLUMN."
  104.   (interactive "r\nP")
  105.   (if (null column)
  106.       (if fill-prefix
  107.       (save-excursion
  108.         (goto-char end)
  109.         (setq end (point-marker))
  110.         (goto-char start)
  111.         (let ((regexp (regexp-quote fill-prefix)))
  112.         (while (< (point) end)
  113.           (or (looking-at regexp)
  114.                   (and (bolp) (eolp))
  115.           (insert fill-prefix))
  116.           (forward-line 1))))
  117.     (if indent-region-function
  118.         (funcall indent-region-function start end)
  119.       (save-excursion
  120.       (goto-char end)
  121.       (setq end (point-marker))
  122.       (goto-char start)
  123.       (or (bolp) (forward-line 1))
  124.       (while (< (point) end)
  125.             (or (and (bolp) (eolp))
  126.                 (funcall indent-line-function))
  127.         (forward-line 1))
  128.       (move-marker end nil))))
  129.     (setq column (prefix-numeric-value column))
  130.     (save-excursion
  131.       (goto-char end)
  132.       (setq end (point-marker))
  133.       (goto-char start)
  134.       (or (bolp) (forward-line 1))
  135.       (while (< (point) end)
  136.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  137.     (or (eolp)
  138.     (indent-to column 0))
  139.     (forward-line 1))
  140.       (move-marker end nil))))
  141.  
  142. (defun indent-relative-maybe ()
  143.   "Indent a new line like previous nonblank line."
  144.   (interactive)
  145.   (indent-relative t))
  146.  
  147. (defun indent-relative (&optional unindented-ok)
  148.   "Space out to under next indent point in previous nonblank line.
  149. An indent point is a non-whitespace character following whitespace.
  150. If the previous nonblank line has no indent points beyond
  151. the column point starts at, `tab-to-tab-stop' is done instead."
  152.   (interactive "P")
  153.   (if abbrev-mode (expand-abbrev))
  154.   (let ((start-column (current-column))
  155.     indent)
  156.     (save-excursion
  157.       (beginning-of-line)
  158.       (if (re-search-backward "^[^\n]" nil t)
  159.       (let ((end (save-excursion (forward-line 1) (point))))
  160.         (move-to-column start-column)
  161.         ;; Is start-column inside a tab on this line?
  162.         (if (> (current-column) start-column)
  163.         (backward-char 1))
  164.         (or (looking-at "[ \t]")
  165.         unindented-ok
  166.         (skip-chars-forward "^ \t" end))
  167.         (skip-chars-forward " \t" end)
  168.         (or (= (point) end) (setq indent (current-column))))))
  169.     (if indent
  170.     (let ((opoint (point-marker)))
  171.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  172.       (indent-to indent 0)
  173.       (if (> opoint (point))
  174.           (goto-char opoint))
  175.       (move-marker opoint nil))
  176.       (tab-to-tab-stop))))
  177.  
  178. (defvar tab-stop-list
  179.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  180.   "*List of tab stop positions used by `tab-to-tab-stops'.
  181. This should be a list of integers, ordered from smallest to largest.")
  182.  
  183. (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
  184. (if edit-tab-stops-map
  185.     nil
  186.   (setq edit-tab-stops-map (make-sparse-keymap))
  187.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  188.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  189.  
  190. (defvar edit-tab-stops-buffer nil
  191.   "Buffer whose tab stops are being edited--in case
  192. the variable `tab-stop-list' is local in that buffer.")
  193.  
  194. (defun edit-tab-stops ()
  195.   "Edit the tab stops used by `tab-to-tab-stop'.
  196. Creates a buffer *Tab Stops* containing text describing the tab stops.
  197. A colon indicates a column where there is a tab stop.
  198. You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
  199.   (interactive)
  200.   (setq edit-tab-stops-buffer (current-buffer))
  201.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  202.   ;; #### I18N3 should mark buffer as output-translating
  203.   (use-local-map edit-tab-stops-map)
  204.   (make-local-variable 'indent-tabs-mode)
  205.   (setq indent-tabs-mode nil)
  206.   (overwrite-mode 1)
  207.   (setq truncate-lines t)
  208.   (erase-buffer)
  209.   (let ((tabs tab-stop-list))
  210.     (while tabs
  211.       (indent-to (car tabs) 0)
  212.       (insert ?:)
  213.       (setq tabs (cdr tabs))))
  214.   (let ((count 0))
  215.     (insert ?\n)
  216.     (while (< count 8)
  217.       (insert (+ count ?0))
  218.     (insert "         ")
  219.       (setq count (1+ count)))
  220.     (insert ?\n)
  221.     (while (> count 0)
  222.       (insert "0123456789")
  223.       (setq count (1- count))))
  224.   (insert (substitute-command-keys "\nTo install changes, type \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes]"))
  225.   (goto-char (point-min)))
  226.  
  227. (defun edit-tab-stops-note-changes ()
  228.   "Put edited tab stops into effect."
  229.   (interactive)
  230.     (let (tabs)
  231.       (save-excursion
  232.     (goto-char 1)
  233.     (end-of-line)
  234.     (while (search-backward ":" nil t)
  235.       (setq tabs (cons (current-column) tabs))))
  236.       (bury-buffer (prog1 (current-buffer)
  237.               (switch-to-buffer edit-tab-stops-buffer)))
  238.       (setq tab-stop-list tabs))
  239.   (message "Tab stops installed"))
  240.  
  241. (defun tab-to-tab-stop ()
  242.   "Insert spaces or tabs to next defined tab-stop column.
  243. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  244. Use \\[edit-tab-stops] to edit them interactively."
  245.   (interactive)
  246.   (if abbrev-mode (expand-abbrev))
  247.   (let ((tabs tab-stop-list))
  248.     (while (and tabs (>= (current-column) (car tabs)))
  249.       (setq tabs (cdr tabs)))
  250.     (if tabs
  251.     (let ((opoint (point)))
  252.       (skip-chars-backward " \t")
  253.       (delete-region (point) opoint)
  254.       (indent-to (car tabs)))
  255.       (insert ?\ ))))
  256.  
  257. (defun move-to-tab-stop ()
  258.   "Move point to next defined tab-stop column.
  259. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  260. Use \\[edit-tab-stops] to edit them interactively."
  261.   (interactive)
  262.   (let ((tabs tab-stop-list))
  263.     (while (and tabs (>= (current-column) (car tabs)))
  264.       (setq tabs (cdr tabs)))
  265.     (if tabs
  266.     (let ((before (point)))
  267.       (move-to-column (car tabs) t)
  268.       (save-excursion
  269.         (goto-char before)
  270.         ;; If we just added a tab, or moved over one,
  271.         ;; delete any superfluous spaces before the old point.
  272.         (if (and (eq (preceding-char) ?\ )
  273.              (eq (following-char) ?\t))
  274.         (let ((tabend (* (/ (current-column) tab-width) tab-width)))
  275.           (while (and (> (current-column) tabend)
  276.                   (eq (preceding-char) ?\ ))
  277.             (forward-char -1))
  278.           (delete-region (point) before))))))))
  279.  
  280. ;;; indent.el ends here
  281.